package cn.dankal.lequ.ui.fragment.gamedetails_fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.dankal.lequ.R;
import cn.dankal.lequ.commom.adapter.BaseAdapter.ViewHolder;
import cn.dankal.lequ.commom.adapter.BaseAdapter.recyclerview.CommonAdapter;
import cn.dankal.lequ.domain.model.gamedetails.GameDetailsCommentModel;


/**
 * 作者： fred on 16/4/19 11:21.
 * 邮箱：fred_kang@dankal.cn
 */
public class Comment_Fragment extends Fragment {
    @Bind(R.id.tv_good_number)
    TextView tv_good_number;
    @Bind(R.id.tv_bad_number)
    TextView tv_bad_number;
    @Bind(R.id.iv_good)
    ImageView iv_good;
    @Bind(R.id.iv_bad)
    ImageView iv_bad;
    @Bind(R.id.pb)
    ProgressBar pb;
    @Bind(R.id.tv_goodrate)
    TextView tv_goodrate;

    //    @Bind(R.id.tv_username) TextView tv_username;
//    @Bind(R.id.tv_time) TextView tv_time;
//    @Bind(R.id.tv_comment) TextView tv_comment;
//
    @Bind(R.id.recyclerview)
    RecyclerView recyclerview;

    LinearLayoutManager linearLayoutManager;

    private boolean isGoodClick=false,isBadClick=false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gamedetails_comment, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //获取进度并且判断当前用户是点赞还是差评，根据情况改变默认的图标，参考衣起穿首页
        int a = Integer.parseInt(tv_good_number.getText().toString());
        int b = Integer.parseInt(tv_bad_number.getText().toString());
        tv_goodrate.setText(String.valueOf(progress(a, b)) + "%");
        pb.setProgress(progress(a,b));

        initRecyclerView();
    }

    private void initRecyclerView() {
        linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerview.setLayoutManager(linearLayoutManager);
        recyclerview.setHasFixedSize(true);
        List<GameDetailsCommentModel> mDatas = new ArrayList<GameDetailsCommentModel>();
        GameDetailsCommentModel commentModel = new GameDetailsCommentModel();
        commentModel.setComment("王尼玛我要给你生猴子王尼玛我要给你生猴子王尼玛我要给你生猴子王尼玛我要给你生猴子");
        commentModel.setTime("21分钟前");
        commentModel.setUser_name("王尼玛");
        mDatas.add(commentModel);
        mDatas.add(commentModel);
        mDatas.add(commentModel);
        mDatas.add(commentModel);
        mDatas.add(commentModel);
        mDatas.add(commentModel);

        recyclerview.setAdapter(new CommonAdapter<GameDetailsCommentModel>(getActivity(), R.layout.fragment_gamedetails_comment_item, mDatas) {
            @Override
            public void convert(ViewHolder holder, GameDetailsCommentModel commentModel) {
                holder.setText(R.id.tv_comment, commentModel.getComment());
                holder.setText(R.id.tv_username, commentModel.getUser_name());
                holder.setText(R.id.tv_time, commentModel.getTime());

            }
        });
    }


    @OnClick({R.id.iv_good,R.id.iv_bad})
    public void imageViewClick(View view) {
        switch (view.getId()) {
            case R.id.iv_good:
                int a = Integer.parseInt(tv_good_number.getText().toString());
                if (isGoodClick==false){
                isGoodClick=true;
                    a++;
                    iv_good.setImageResource(R.mipmap.ic_good_pressed);
                    iv_bad.setClickable(false);
                }
                else{
                isGoodClick=false;
                    a--;
                    iv_good.setImageResource(R.mipmap.ic_good);
                    iv_bad.setClickable(true);
                }
                tv_good_number.setText(String.valueOf(a));
                int b = Integer.parseInt(tv_bad_number.getText().toString());
                pb.setProgress(progress(a,b));
                tv_goodrate.setText(String.valueOf(progress(a, b)) + "%");

            break;
            case R.id.iv_bad:
                int a1 = Integer.parseInt(tv_bad_number.getText().toString());
                if (isBadClick==false){
                    isBadClick=true;
                    a1++;
                    iv_bad.setImageResource(R.mipmap.ic_bad_pressed);
                    iv_good.setClickable(false);
                }
                else{
                    isBadClick=false;
                    a1--;
                    iv_bad.setImageResource(R.mipmap.ic_bad);
                    iv_good.setClickable(true);
                }
                tv_bad_number.setText(String.valueOf(a1));
                int b1 = Integer.parseInt(tv_good_number.getText().toString());
                pb.setProgress(progress(b1,a1));
                tv_goodrate.setText(String.valueOf(progress(b1, a1)) + "%");
                break;
            default:
                break;
        }
    }

    private int progress(int a, int b) {
        if (a!=0&&b==0)
            return 100;
        else if (a==0)
            return 0;

        else {
            float persent=(float) a/((float)a+(float) b);
        return (int) (100*persent);
        }
    }


    private boolean getFromServer() {

//        tv_good_number.setText();
//        tv_bad_number.setText();
        return false;
    }
}
